//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)


using NUnit.Framework;
using SoapTrace.Core.Communication;
using System;

namespace SoapTrace.Tests.TcpPacketMonitoring
{
	/// <summary>
	/// Tests the <see cref="TcpConnection.IsSequenceNumberInRange"/>
	/// method.
	/// </summary>
	[ TestFixture ]
	public class SequenceNumberRangeTestFixture
	{
		/// <summary>
		/// Simple test.
		/// </summary>
		[ Test ]
		public void SimpleRangeTest1( )
		{
			uint CurrentSequenceNumber = 0;
			uint Range = 10;
			uint PacketSequenceNumber = 1;
			Assert.IsTrue( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should be in range." );
		}
		
		/// <summary>
		/// Simple test.
		/// </summary>
		[ Test ]
		public void SimpleRangeTest2( )
		{
			Assert.IsFalse( TcpConnection.IsSequenceNumberInRange( 11, 0, 10 ),
			              "Should not be in range." );
		}
		
		/// <summary>
		/// Simple test.
		/// </summary>
		[ Test ]
		public void SimpleRangeTest3( )
		{
			uint CurrentSequenceNumber = 0;
			uint Range = 10;
			uint PacketSequenceNumber = 10;
			Assert.IsFalse( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should not be in range." );
		}
		
		/// <summary>
		/// If the packet sequence number equals the current
		/// sequence number then it is in range.
		/// </summary>
		[ Test ]
		public void SimpleRangeTest4( )
		{
			uint CurrentSequenceNumber = 0;
			uint Range = 10;
			uint PacketSequenceNumber = 0;
			Assert.IsTrue( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should be in range." );
		}	
		
		/// <summary>
		/// Simple test.
		/// </summary>
		[ Test ]
		public void SimpleRangeTest5( )
		{
			uint CurrentSequenceNumber = 0;
			uint Range = 10;
			uint PacketSequenceNumber = 9;
			Assert.IsTrue( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should be in range." );
		}		
		
		/// <summary>
		/// Rollover test.
		/// </summary>
		[ Test ]
		public void RolloverTest1( )
		{
			uint CurrentSequenceNumber = 0xFFFFFFFE;
			uint Range = 10;
			uint PacketSequenceNumber = 0;
			Assert.IsTrue( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should be in range." );
		}	
		
		/// <summary>
		/// Rollover test.
		/// </summary>
		[ Test ]
		public void RolloverTest2( )
		{
			uint CurrentSequenceNumber = 0xFFFFFFFE;
			uint Range = 10;
			uint PacketSequenceNumber = 0xFFFFFFFF;
			Assert.IsTrue( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should be in range." );
		}	
		
		/// <summary>
		/// Rollover test.
		/// </summary>
		[ Test ]
		public void RolloverTest3( )
		{
			uint CurrentSequenceNumber = 0xFFFFFFFE;
			uint Range = 10;
			uint PacketSequenceNumber = 9;
			Assert.IsFalse( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should not be in range." );
		}		
		
		/// <summary>
		/// Rollover test.
		/// </summary>
		[ Test ]
		public void RolloverTest4( )
		{
			uint CurrentSequenceNumber = 0xFFFFFFFE;
			uint Range = 10;
			uint PacketSequenceNumber = 8;
			Assert.IsFalse( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should not be in range." );
		}	
		
		/// <summary>
		/// Rollover test.
		/// </summary>
		[ Test ]
		public void RolloverTest5( )
		{
			uint CurrentSequenceNumber = 0xFFFFFFFE;
			uint Range = 10;
			uint PacketSequenceNumber = 7;
			Assert.IsTrue( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should be in range." );
		}		
		
		/// <summary>
		/// If the packet sequence number equals the current
		/// sequence number then it is in range.
		/// </summary>
		[ Test ]
		public void LowerPacketSequenceNumber1( )
		{
			uint CurrentSequenceNumber = 5;
			uint Range = 10;
			uint PacketSequenceNumber = 1;
			Assert.IsFalse( TcpConnection.IsSequenceNumberInRange( PacketSequenceNumber, CurrentSequenceNumber, Range ),
			              "Should not be in range." );
		}	
	}
}
